package com.quan.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.quan.common.core.BaseService;
import com.quan.common.utils.RandomUitl;
import com.quan.elasticjob.handler.ElasticJobHandler;
import com.quan.system.entity.JobTask;
import com.quan.system.service.JobTaskService;

/**
 * <p>
 * 定时任务 服务实现类
 * </p>
 *
 * @author 杨浩泉
 * @since 2018-09-14
 */
@Service
public class JobTaskServiceImpl extends BaseService<JobTask> implements JobTaskService {
	
	@Autowired
	private ElasticJobHandler elasticJobHandler ;
	
	@Override
	public JobTask insertSelective(JobTask entity) {
		
		entity.setJobName(entity.getJobName() + "-" + RandomUitl.uuid());
		
		JobTask insertSelective = super.insertSelective(entity) ;
		
		//添加任务，并启动
		this.elasticJobHandler.addJob(entity.getJobName(), entity.getCron(), 1, entity.getJobClass(), insertSelective.getId()+"");
		
		return insertSelective;
	}

	@Override
	public void uploadJobStatus(JobTask entity) {
		JobTask t = new JobTask() ;
		t.setId(entity.getId());
		
		JobTask task = super.selectOne(t) ;
		task.setTaskId(entity.getTaskId());
		task.setStatus(1);
		super.updateSelective(task) ;
	}
	
}